Public Class MyInkPicture
    Inherits PictureBox

    Private RightMouse As Boolean = False

    Public WithEvents InkBox As New Microsoft.Ink.InkOverlay
    Public Property InkEnabled() As Boolean
        Get
            Return InkBox.Enabled
        End Get
        Set(ByVal Value As Boolean)
            InkBox.Enabled = Value
        End Set
    End Property

    Public ReadOnly Property MinX() As Integer
        Get
            Dim r As System.Drawing.Rectangle
            r = InkBox.Ink.GetBoundingBox

            Dim TopLeft As Point = r.Location
            Dim BottomRight As Point = New Point(r.Right, r.Bottom)
            Dim G As Graphics = CreateGraphics()
            InkBox.Renderer.InkSpaceToPixel(G, TopLeft)
            InkBox.Renderer.InkSpaceToPixel(G, BottomRight)
            G.Dispose()
            Dim NewR As New Rectangle(TopLeft, New Size(BottomRight.X + 50 - TopLeft.X, BottomRight.Y + 50 - TopLeft.Y))
            Dim X As Integer = 0
            For Each c As PictureBox In Me.Controls
                If c.Location.X + c.Width > X Then X = c.Location.X + c.Width
            Next

            If Not Me.Image Is Nothing Then
                If Me.Image.Width > X Or Me.Image.Width > NewR.Right Then
                    Return Me.Image.Width
                Else
                    If X > NewR.Right Then
                        Return X
                    Else
                        Return NewR.Right
                    End If
                End If
            Else
                If X > NewR.Right Then
                    Return X
                Else
                    Return NewR.Right
                End If
            End If

        End Get
    End Property

    Public ReadOnly Property MinY() As Integer
        Get
            Dim r As System.Drawing.Rectangle
            r = InkBox.Ink.GetBoundingBox

            Dim TopLeft As Point = r.Location
            Dim BottomRight As Point = New Point(r.Right, r.Bottom)
            Dim G As Graphics = CreateGraphics()
            InkBox.Renderer.InkSpaceToPixel(G, TopLeft)
            InkBox.Renderer.InkSpaceToPixel(G, BottomRight)
            G.Dispose()
            Dim NewR As New Rectangle(TopLeft, New Size(BottomRight.X + 50 - TopLeft.X, BottomRight.Y + 50 - TopLeft.Y))
            Dim Y As Integer = 0
            For Each c As PictureBox In Me.Controls
                If c.Location.Y + c.Height > y Then y = c.Location.Y + c.Height
            Next

            If Not Me.Image Is Nothing Then
                If Me.Image.Height > Y Or Me.Image.Height > NewR.Bottom Then
                    Return Me.Image.Height
                Else
                    If Y > NewR.Bottom Then
                        Return Y
                    Else
                        Return NewR.Bottom
                    End If
                End If
            Else
                If Y > NewR.Bottom Then
                    Return Y
                Else
                    Return NewR.Bottom
                End If
            End If
        End Get
    End Property
    Public Sub New()
        Me.SizeMode = PictureBoxSizeMode.Normal
        InkBox.Handle = Me.Handle
        InkBox.Enabled = True
    End Sub
    Public Event Stroke(ByVal sender As Object, ByVal e As Microsoft.Ink.InkCollectorStrokeEventArgs)
    Public Event NewInAirPackets(ByVal sender As Object, ByVal e As Microsoft.Ink.InkCollectorNewInAirPacketsEventArgs)
    Public Event StrokesdDeleted(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Event StrokesDeleting(ByVal sender As Object, ByVal e As Microsoft.Ink.InkOverlayStrokesDeletingEventArgs)

    Private Sub InkBox_Stroke(ByVal sender As Object, ByVal e As Microsoft.Ink.InkCollectorStrokeEventArgs) Handles InkBox.Stroke
        If e.Cursor.Name = "Mouse" And RightMouse Then
            e.Cancel = True
        End If
        If InkBox.EditingMode = Microsoft.Ink.InkOverlayEditingMode.Ink Then
            RaiseEvent Stroke(sender, e)
        End If
        RightMouse = False
    End Sub

    Private Sub InkBox_StrokesDeleted(ByVal sender As Object, ByVal e As System.EventArgs) Handles InkBox.StrokesDeleted
        RaiseEvent StrokesdDeleted(sender, e)
    End Sub

    Private Sub InkBox_StrokesDeleting(ByVal sender As Object, ByVal e As Microsoft.Ink.InkOverlayStrokesDeletingEventArgs) Handles InkBox.StrokesDeleting
        RaiseEvent StrokesDeleting(sender, e)
    End Sub

    Private Sub InkBox_NewInAirPackets(ByVal sender As Object, ByVal e As Microsoft.Ink.InkCollectorNewInAirPacketsEventArgs) Handles InkBox.NewInAirPackets
        RaiseEvent NewInAirPackets(sender, e)
    End Sub

    Public Overloads WriteOnly Property Image(ByVal Resize As Boolean) As System.Drawing.Image
        Set(ByVal Value As System.Drawing.Image)
            Me.Image = Value
            If Resize And Not (Value Is Nothing) Then
                Dim m As frmDrawing = Me.Parent
                Dim X As Integer = MinX
                Dim Y As Integer = MinY
                If m.Width > X Then X = m.Width
                If m.Height > y Then y = m.Height
                Me.Size = New Size(X, y)
            End If
        End Set
    End Property



    Private Sub InkBox_MouseDown(ByVal sender As Object, ByVal e As Microsoft.Ink.CancelMouseEventArgs) Handles InkBox.MouseDown
        If e.Button = 2 Then

            RightMouse = True
        End If
    End Sub


End Class
